//
//  STFeedListServiceManager.swift
//  Story
//
//  Created by 汤泽川 on 2019/4/22.
//  Copyright © 2019 汤泽川. All rights reserved.
//

import Foundation
import RxCocoa
import RxSwift

protocol STStoryServiceManager {
    func refresh() -> Observable<[STStoryInfo]>
    func loadMore() -> Observable<[STStoryInfo]>
}

class STFeedListServiceManager: STStoryServiceManager {

    let channelId : Int
    var page = 0
    let pageCount : Int
    let storyService = STStoryService()
    
    init(channelId : Int, pageCount : Int = 20) {
        self.channelId = channelId
        self.pageCount = pageCount
    }
    
    func refresh() -> Observable<[STStoryInfo]> {
        let publishSubject = ReplaySubject<[STStoryInfo]>.create(bufferSize: 1)
        storyService.fetchStoryInfo(channelId: self.channelId, page: 0, count: self.pageCount).result { (response, error) in
            if error != nil {
                publishSubject.onNext([])
            }
            if let data = response?.data {
                self.page = 0
                publishSubject.onNext(data)
            } else {
                publishSubject.onNext([])
            }
        }
        return publishSubject
    }
    
    func loadMore() -> Observable<[STStoryInfo]> {
        let publishSubject = ReplaySubject<[STStoryInfo]>.create(bufferSize: 1)
        storyService.fetchStoryInfo(channelId: self.channelId, page: self.page + 1, count: self.pageCount).result { (response, error) in
            if error != nil {
                publishSubject.onNext([])
            }
            if let data = response?.data {
                self.page = self.page + 1
                publishSubject.onNext(data)
            } else {
                publishSubject.onNext([])
            }
        }
        return publishSubject
    }
    
}

class STSearchListServiceManager: STStoryServiceManager {
    
    var keyword : String?
    var page = 0
    let pageCount : Int
    let storyService = STStoryService()
    
    init(pageCount : Int = 20) {
        self.pageCount = pageCount
    }
    
    func refresh() -> Observable<[STStoryInfo]> {
        let publishSubject = ReplaySubject<[STStoryInfo]>.create(bufferSize: 1)
        if self.keyword == nil {
            publishSubject.onNext([])
        } else {
            storyService.searchStoryInfo(keyword: self.keyword ?? "", page: 0, count: self.pageCount).result { (response, error) in
                if error != nil {
                    publishSubject.onNext([])
                }
                if let data = response?.data {
                    self.page = 0
                    publishSubject.onNext(data)
                } else {
                    publishSubject.onNext([])
                }
            }
        }
        return publishSubject
    }
    
    func loadMore() -> Observable<[STStoryInfo]> {
        let publishSubject = ReplaySubject<[STStoryInfo]>.create(bufferSize: 1)
        if self.keyword == nil {
            publishSubject.onNext([])
        } else {
            storyService.searchStoryInfo(keyword: self.keyword ?? "", page: self.page + 1, count: self.pageCount).result { (response, error) in
                if error != nil {
                    publishSubject.onNext([])
                }
                if let data = response?.data {
                    self.page = self.page + 1
                    publishSubject.onNext(data)
                } else {
                    publishSubject.onNext([])
                }
            }
        }
        return publishSubject
    }
    
}

class STSearchAuthorListServiceManager: STStoryServiceManager {
    
    var authorName : String?
    var page = 0
    let pageCount : Int
    let storyService = STStoryService()
    
    init(pageCount : Int = 20) {
        self.pageCount = pageCount
    }
    
    func refresh() -> Observable<[STStoryInfo]> {
        let publishSubject = ReplaySubject<[STStoryInfo]>.create(bufferSize: 1)
        if self.authorName == nil {
            publishSubject.onNext([])
        } else {
            storyService.searchAuthorStoryInfo(authorName: self.authorName ?? "", page: 0, count: self.pageCount).result { (response, error) in
                if error != nil {
                    publishSubject.onNext([])
                }
                if let data = response?.data {
                    self.page = 0
                    publishSubject.onNext(data)
                } else {
                    publishSubject.onNext([])
                }
            }
        }
        return publishSubject
    }
    
    func loadMore() -> Observable<[STStoryInfo]> {
        let publishSubject = ReplaySubject<[STStoryInfo]>.create(bufferSize: 1)
        if self.authorName == nil {
            publishSubject.onNext([])
        } else {
            storyService.searchAuthorStoryInfo(authorName: self.authorName ?? "", page: self.page + 1, count: self.pageCount).result { (response, error) in
                if error != nil {
                    publishSubject.onNext([])
                }
                if let data = response?.data {
                    self.page = self.page + 1
                    publishSubject.onNext(data)
                } else {
                    publishSubject.onNext([])
                }
            }
        }
        return publishSubject
    }
    
}

class STCollectListServiceManager: STStoryServiceManager {
    var page = 0
    let pageCount : Int
    let storyService = STCollectService()
    
    init(pageCount : Int = 20) {
        self.pageCount = pageCount
    }
    
    func refresh() -> Observable<[STStoryInfo]> {
        let publishSubject = ReplaySubject<[STStoryInfo]>.create(bufferSize: 1)
        if STUserService.shareInstance.currentUser.value == nil {
            publishSubject.onNext([])
        } else {
            storyService.fetchCollectStorys(page: 0, count: self.pageCount).result { (response, error) in
                if error != nil {
                    publishSubject.onNext([])
                }
                if let data = response?.data {
                    self.page = 0
                    publishSubject.onNext(data)
                } else {
                    publishSubject.onNext([])
                }
            }
        }
        return publishSubject
    }
    
    func loadMore() -> Observable<[STStoryInfo]> {
        let publishSubject = ReplaySubject<[STStoryInfo]>.create(bufferSize: 1)
        if STUserService.shareInstance.currentUser.value == nil {
            publishSubject.onNext([])
        } else {
            storyService.fetchCollectStorys(page: self.page + 1, count: self.pageCount).result { (response, error) in
                if error != nil {
                    publishSubject.onNext([])
                }
                if let data = response?.data {
                    self.page = self.page + 1
                    publishSubject.onNext(data)
                } else {
                    publishSubject.onNext([])
                }
            }
        }
        return publishSubject
    }
    
}
